<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Card extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getCardsPage()
    {
        $storeAndRoomInfo = model('Store')->with('rooms')->select();
        // 过滤掉房间状态为下线的
        $newStoreAndRoomInfo = [];
        foreach ($storeAndRoomInfo as $sr) {
            $haveOne = false;
            if ($sr->rooms && count($sr->rooms)) {
                foreach ($sr->rooms as $r) {
                    if ($r->status == 'normal') {
                        $haveOne = true;
                    }
                }
            }
            if ($haveOne) {
                $newStoreAndRoomInfo[] = $sr;
            }
        }

//        var_dump($storeAndRoomInfo);

        $params = $this->request->get();

        $cards = model('Card')->where('card_display', 1)->order('weigh', 'desc')->select();
        $cards = array_map(function ($e) {
            // $rooms = \app\admin\model\Room::all();
            // 增加过滤状态
            $rooms = \app\admin\model\Room::where('status', 'normal')->select();
            $roomIds = array_map(function ($e) {
                return $e['id'];
            }, $rooms);

            $setRoomIds = explode(',', $e->store_ids);
            // temp 还原store_ids存储为门店id,便于前端筛选
            // 增加过滤状态
            $rooms = \app\admin\model\Room::where('id', 'in', $setRoomIds)->where('status', 'normal')->select();
            $storeIds = array_map(function ($e) {
                return $e['store_id'];
            }, $rooms);
            // 去重下
            $storeIds = array_flip($storeIds);
            $storeIds = array_flip($storeIds);

            // 过滤状态的教室id
            $filterRoomIds = array_map(function ($e) {
                return $e['id'];
            }, $rooms);

            $count = count($filterRoomIds);
            if ($count == count($roomIds)) {
                $e->display_store_string = '适用全部教室';
            } else {
                $e->display_store_string = "适用${count}个教室";
            }
            $e->store_ids = "'" . implode("','", $storeIds) . "'";

            return $e;
        }, $cards);
        $this->success('ok', [
            'store_and_room_info' => $newStoreAndRoomInfo,
            'cards' => $cards,
        ]);
    }

    public function list()
    {
        $params = $this->request->get();
        $data = model('Card')->with(['store', 'room'])->select();
//        var_dump($data);

//        $cardInfo = array_map(function ($e) {
//            return [
//                'id' => $e->id,
//                'name' => $e->card_name,
//                'price' => $e->card_price,
//                'store' => '适用门店:' . is_null($e->store) ? '通用' : $e->store->store_name,
//                'room' => '适用教室：通用',
//                'card_checknum' => '卡：12次',
//                'card_max_hour' => '每次时长：2小时',
//            ];
//        }, $data);
        $this->success('ok', $data);

    }

    public function getCardInfo()
    {
        $id = $this->request->get('id');
        $data = model('Card')->where('id', $id)->find();
        // 增加状态过滤
        $rooms = \app\admin\model\Room::where('status', 'normal')->with('store')->select();
        $roomIds = array_map(function ($e) {
            return $e['id'];
        }, $rooms);

        $roomNameMap = [];
        foreach ($rooms as $v) {
            $roomNameMap[$v['id']] = $v['store']['store_name'] . ' - ' . $v['room_name'];
        }
        $setRoomIds = explode(",", $data->store_ids);
        // 增加过滤状态
        $rooms = \app\admin\model\Room::where('id', 'in', $setRoomIds)->where('status', 'normal')->select();
        $storeIds = array_map(function ($e) {
            return $e['store_id'];
        }, $rooms);

        $filterRoomIds = array_map(function ($e) {
            return $e['id'];
        }, $rooms);
        $count = count($filterRoomIds);
 
        $roomsList = [];
        if ($count == count($roomIds)) {
            $data->display_store_string = '适用全部教室';
            $data->store_list_string = '适用全部教室';
            $roomsList[] = '适用全部教室';
        } else {
            $data->display_store_string = "适用${count}个教室";
            $data->store_list_string = '';

            $ids = explode(',', $data->store_ids);
            foreach ($ids as $v) {
                if (isset($roomNameMap[$v])) {
                    $data->store_list_string .= $roomNameMap[$v] . "\n";
                    $roomsList[] = $roomNameMap[$v];
                }
            }
        }
        $data->rooms_list = $roomsList;

        $this->success('ok', $data);

    }
}
